package com.nine.pesys.model.action;

import com.nine.pesys.base.BaseAction;
import com.nine.pesys.model.entity.Equipment;
import com.nine.pesys.model.entity.EquipmentDetail;
import com.nine.pesys.util.annotation.Privilege;
import com.nine.pesys.util.common.PageData;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * Created by L先森 on 2014/12/25.
 */
@Controller
@Scope("prototype")
@Namespace("/equipmentDetail")
@ParentPackage("my_struts")
@Results({
        @Result(name = "equipmentRair",location = "/front/home.jsp"),
        @Result(name = "equipmentDel",location = "/front/home.jsp"),

        @Result(name = "bsAllEq",location = "/admin/equipment/equipment_list.jsp"),
        @Result(name = "addEquipment",type = "redirectAction",location = "/allEq.do"),
})
@Privilege
public class EquipmentDetailAction extends BaseAction<EquipmentDetail> {
    private String equipmentName;
    private Float price;
    private Long[] equipmentDetailIds;

    @Action("/allEq")
    public String allEquipment() throws Exception{
        list = this.equipmentDetailService.findAll();
        if (list!=null&&list.size()>0) {
            System.out.println(list.size()+"===="+pageNo+"====="+list);
            PageData<EquipmentDetail> pageData = new PageData<EquipmentDetail>(list.size(), pageNo, pageSize, list);
            if (pageNo>pageData.getTotalPageCount())
                pageNo = pageData.getTotalPageCount();
//            equipments = this.equipmentService.getByPage(pageSize,pageNo);
            list = this.equipmentDetailService.getByPage(pageSize,pageNo);
            ActionContext.getContext().put("eqDL",list);
            ActionContext.getContext().put("pageData",pageData);
        }
        return "bsAllEq";
    }

    /**
     * 器材新增
     *  如果之前有这个器材，就数目加一
     *  没有就创建一个新的。价格以第一次输入的价格为准
     * @return
     * @throws Exception
     */
    @Action("/addEquipment")
    @Privilege("system")
    public String addEquipment() throws Exception{
        Equipment equipment = new Equipment();
        if (equipmentName!=null)
             equipment.setName(equipmentName);
        equipments = this.equipmentService.getByCondition(equipment);
        if (equipments!=null&&equipments.size()>0){
            equipment = equipments.get(0);
            equipment.setTotalNum(equipment.getTotalNum()+1L);
            equipment.setLeftNum(equipment.getLeftNum()+1L);
            this.equipmentService.update(equipment);

        }else {
            System.out.println("===============新建========");
            equipment = new Equipment();
            equipment.setName(equipmentName);
            equipment.setPrice(price);
            equipment.setTotalNum(1L);
            equipment.setLeftNum(1L);
            this.equipmentService.save(equipment);
        }
        model.setEquipment(equipment);
        this.equipmentDetailService.save(model);
        return "addEquipment";
    }

    /**
     * 器材维修(支持批量维修)
     * @return
     * @throws Exception
     */
    @Privilege("super")
    @Action("/equipmentRair")
    public String equipmentRair() throws Exception{
        equipmentDetails = this.equipmentDetailService.findByIds(equipmentDetailIds);
        if(equipmentDetails!=null&&equipmentDetails.size()>0){
            for(EquipmentDetail equipmentDetail:equipmentDetails){
                equipmentDetail.setIsRair(true);
                Equipment equipment = equipmentDetail.getEquipment();
                equipment.setLeftNum(equipment.getLeftNum()-1);
                this.equipmentService.update(equipment);
            }
        }
        return "equipmentRair";
    }

    /**
     * 器材删除
     * @return
     * @throws Exception
     */
    @Privilege("super")
    @Action("/equipmentDel")
    public String equipmentDel() throws Exception{
        equipmentDetails = this.equipmentDetailService.findByIds(equipmentDetailIds);
        if(equipmentDetails!=null&&equipmentDetails.size()>0){
            for(EquipmentDetail equipmentDetail:equipmentDetails){
                Equipment equipment = equipmentDetail.getEquipment();
                equipment.getEquipmentDetails().remove(equipmentDetail);
                equipment.setLeftNum(equipment.getLeftNum()-1);
                equipment.setTotalNum(equipment.getTotalNum()-1);
                this.equipmentService.update(equipment);
            }
        }
        return "equipmentDel";
    }



    public Long[] getEquipmentDetailIds() {
        return equipmentDetailIds;
    }

    public void setEquipmentDetailIds(Long[] equipmentDetailIds) {
        this.equipmentDetailIds = equipmentDetailIds;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getEquipmentName() {
        return equipmentName;
    }

    public void setEquipmentName(String equipmentName) {
        this.equipmentName = equipmentName;
    }
}
